package com.toivan.mt.model;

import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.sdk.MtSDK;
import java.io.File;
import com.toivan.mt.utils.FileUtils;

/**
 * @ClassName MtGreenScreen
 * @Description 绿幕的item
 * @Author yang
 * @Date 2021/6/23 10:30
 */
public class MtGreenScreen {

    private String name;
    private String dir;
    private String category;
    private String thumb;

    public static final MtGreenScreen NO_GREEN_SCREEN = new MtGreenScreen("", "", "", "", false, true);

    public boolean isFromDisk() {
        return isFromDisk;
    }

    public void setFromDisk(boolean fromDisk, Context context, final String sourcePath) {
        isFromDisk = fromDisk;
        if (isFromDisk) {
            MtGreenScreenManager manager = MtJsonParser.getInstance().getGreenScreenManager();
            if (manager != null) {
                Log.i("添加图片：", sourcePath);
                //根据地址读取源文件
                File sourceFile = new File(sourcePath);
                //获取最后一个.的位置
                int lastIndexOf = sourcePath.lastIndexOf(".");
                //获取文件的后缀名 .jpg
                String suffix = sourcePath.substring(lastIndexOf);
                //拼接出新的文件名
                String newFileName = (System.currentTimeMillis() / 1000) + "";
                //目标位置和文件
                File targetFile = new File(MtSDK.get().getGreenScreenPath(), newFileName + suffix);
                //文件复制
                if (FileUtils.copyFile(sourceFile, targetFile)) {
                    Log.i("复制绿幕背景文件：", "成功");
                    this.dir = targetFile.getAbsolutePath();
                } else {
                    Log.e("复制绿幕背景文件：", "失败");
                    return;
                }
                //更新配置文件的名称
                this.name = newFileName;

                //持久化
                manager.getGreenscreens().add(this);
                MtJsonParser.getInstance().greenScreenDownload( new Gson().toJson(manager));
            }
        }
    }

    public boolean isDownloaded() {
        return downloaded;
    }


    private boolean voiced;
    private boolean downloaded;

    private boolean isFromDisk = false;

    public MtGreenScreen(String name, String dir, String category, String thumb, boolean voiced, boolean downloaded) {
        this.name = name;
        this.dir = dir;
        this.category = category;
        this.thumb = thumb;
        this.voiced = voiced;
        this.downloaded = downloaded;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getDir() {
        return dir;
    }


    public void setDir(String dir) {
        this.dir = dir;
    }


    public String getCategory() {
        return category;
    }


    public void setCategory(String category) {
        this.category = category;
    }


    public String getThumb() {
        return MtSDK.get().getGreenScreenUrl() + thumb;
    }


    public void setThumb(String thumb) {
        this.thumb = thumb;
    }


    public String getUrl() {
        return MtSDK.get().getGreenScreenUrl() + dir + ".png";
    }


    public boolean isVoiced() {
        return voiced;
    }


    public void setVoiced(boolean voiced) {
        this.voiced = voiced;
    }


    public boolean getDownloaded() {
        return downloaded;
    }

    @Override
    public String toString() {
        return "MtGreenScreen{" +
                "name='" + name + '\'' +
                ", dir='" + dir + '\'' +
                ", category='" + category + '\'' +
                ", thumb='" + thumb + '\'' +
                ", voiced=" + voiced +
                ", downloaded=" + downloaded +
                ", isFromDisk=" + isFromDisk +
                '}';
    }

    public void setDownloaded(boolean downloaded) {
        this.downloaded = downloaded;
    }


    public void greenScreenDelete(Context context) {
        MtGreenScreenManager manager = MtJsonParser.getInstance().getGreenScreenManager();
        manager.getGreenscreens().remove(manager.findGreenScreen(name));
        MtJsonParser.getInstance().greenScreenDownload( new Gson().toJson(manager));
    }

    public void greenScreenDownload(Context context) {
        MtGreenScreenManager manager = MtJsonParser.getInstance().getGreenScreenManager();
        if (manager != null) {
            manager.findGreenScreen(this.name).setDownloaded(true);
            MtJsonParser.getInstance().greenScreenDownload( new Gson().toJson(manager));
        } else {
        }
    }

}
